package com.jokerku.lottery.domain.receive.engine;

import com.jokerku.lottery.domain.receive.filter.EventFilter;
import com.jokerku.lottery.domain.receive.model.BehaviorMatter;
import com.jokerku.lottery.infrastructure.common.EventEnum;
import com.jokerku.lottery.infrastructure.common.EventTypeEnum;

import java.util.Map;
import java.util.Objects;

/**
 * @Author: guzq
 * @CreateTime: 2022/08/31 10:33
 * @Description: 引擎基类
 * @Version: 1.0
 */
public abstract class EngineBase extends EngineConfig implements Engine {

    @Override
    public String process(BehaviorMatter req) {
        throw new RuntimeException("未实现消息引擎服务");
    }

    /**
     * 获取具体的事件处理器
     *
     * @param req 入参
     * @return 事件处理器
     */
    protected EventFilter router(BehaviorMatter req) {
        // 获取处理器
        EventTypeEnum eventTypeEnum = EventTypeEnum.getByStr(req.getMsgType());
        Map<EventEnum, EventFilter> eventFilterMap = EVENT_FILTER_MAP.get(eventTypeEnum);
        if (Objects.isNull(eventFilterMap)) {
            return null;
        }
        // 事件类型
        if (Objects.equals(EventTypeEnum.EVENT, eventTypeEnum)) {
            EventEnum eventEnum = EventEnum.getByStr(req.getEvent());
            return eventFilterMap.get(eventEnum);
        }

        //消息类型
        if (Objects.equals(EventTypeEnum.TEXT, eventTypeEnum)) {
            return eventFilterMap.get(EventEnum.LOTTERY);
        }

        return null;
    }
}
